//
//  RouteViewCell.m
//  DigiOps
//
//  Created by app on 2018/7/17.
//  Copyright © 2018年 俞渊华. All rights reserved.
//

#import "RouteViewCell.h"
#import "RouteScanView.h"
#import "lhScanQCodeViewController.h"
#import "LineDetail.h"

@interface RouteViewCell(){
//    int px;
//    int py;
}
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *nameLabel;

@property(nonatomic, strong)UIImageView *iconView;
/// 时间 来源  标识
//@property(nonatomic, strong)RouteScanView *scanView;

@property(nonatomic,strong) UIButton* remarkLabel1;

@end
@implementation RouteViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifier = @"RouteViewCell";
//    RouteViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (!cell) {
    RouteViewCell * cell = [[RouteViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont xm_medium:15.0];
        self.titleLabel.numberOfLines = 1;
        self.titleLabel.textColor = [UIColor xm_jetblackColor];
        [self addSubview:self.titleLabel];
        
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.font = [UIFont xm_medium:15.0];
        self.nameLabel.numberOfLines = 1;
        self.nameLabel.textColor = [UIColor xm_jetblackColor];
        //自动折行设置
        self.nameLabel.lineBreakMode = UILineBreakModeWordWrap;
        self.nameLabel.numberOfLines = 0;
        [self addSubview:self.nameLabel];
        
        self.iconView = [[UIImageView alloc]init];
        [self addSubview:self.iconView];
        
        
        self.scanView = [[RouteScanView alloc]init];
        self.scanView.isInRoute=YES;
        [self.contentView addSubview:self.scanView];
        
        self.scanView.hidden=YES;
        
        self.remarkView = [[RouteRemarkView alloc]init];
        self.remarkView.block = ^{
            if (self.block) {
                self.block();
            }
        };
        [self addSubview:self.remarkView];
        
        self.remarkLabel1 = [[UIButton alloc]init];
        
        //        self.remarkLabel.textColor = [UIColor xm_colorFromRGB:0x858C96];
        //        self.remarkLabel.textColor=[UIColor blueColor];
        [self.remarkLabel1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.remarkLabel1.titleLabel setFont:[UIFont xm_font:15.0]];
        //        self.remarkLabel.font = [UIFont xm_font:15.0];
        self.remarkLabel1.layer.cornerRadius=2;
        self.remarkLabel1.backgroundColor =[UIColor whiteColor];
        self.remarkLabel1.layer.borderColor=[UIColor darkGrayColor].CGColor;
        self.remarkLabel1.layer.borderWidth=0.3;
        [self.remarkLabel1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.remarkLabel1];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer)];
        [self addGestureRecognizer:tapGesture];
        
    }
    return self;
}
-(void)back{
    if (self.blockError) {
        self.blockError();
    }
}
-(void)tapGestureRecognizer{
//    if (px>self.remarkView.frame.origin.x-2) {
//        if (self.blockError) {
//            self.blockError();
//        }
//    }
//    else{
    if (self.block) {
        self.block();
    }


//    }
//    //    NewsDetailViewController * detailVC = [[NewsDetailViewController alloc]init];
//    //    detailVC.model = self.model;
//    //    [self.viewController.rt_navigationController pushViewController:detailVC animated:YES];
//    lhScanQCodeViewController * sqVC = [[lhScanQCodeViewController alloc]init];
////    UINavigationController * nVC = [[UINavigationController alloc]initWithRootViewController:sqVC];
//    RTRootNavigationController * nav = [[RTRootNavigationController alloc] initWithRootViewController:sqVC];
//
//    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:nav animated:YES completion:^{
//        NSLog(@"%@");
//    }];
////    [self presentViewController:nVC animated:YES completion:^{
////
////    }];
}
//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    NSLog(@"%@",[touch.view class]);
//    CGPoint point = [touch locationInView:[touch view]]; //返回触摸点在视图中的当前坐标
//    int x = point.x;
//    int y = point.y;
//    NSLog(@"touch (x, y) is (%d, %d)", x, y);
//    px=x;
//    py=y;
//    NSLog(@"%f,%f",self.remarkView.frame.origin.x,self.remarkView.frame.origin.y);
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//        return NO;
//    }
//    return YES;
//
//}

-(void)setModel:(RouteModel *)model{
    _model = model;
    LineDetail* l=(LineDetail*)[_model.details objectAtIndex:model.ID];
    self.titleLabel.text = [NSString stringWithFormat: @"检查点号码: %@",l.pointNum ];
    self.nameLabel.text = [NSString stringWithFormat: @"检查点名称: %@",l.pointName];
    self.scanView.model = _model;
    self.scanView.line=l;
    //用line不用model
//    self.remarkView.model = _model;
    self.remarkView.line =l;//此处设置路线的异常备注颜色
    
    if (model.excepDetails.count>0) {
        LineDetail* le=(LineDetail*)[_model.excepDetails objectAtIndex:model.ID];
        self.remarkView.lineF =le;//此处设置路线的异常备注颜色
        self.scanView.lineF=le;
    }
    [self.remarkLabel1 setTitle:@"异常备注" forState:UIControlStateNormal];
    if (model.ID==1) {
        [self.remarkLabel1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    if (model.ID==2||model.ID==3||model.ID==4) {
        [self.remarkLabel1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    //    [self.iconView xm_setImageWithURL:_model.ImgUrl];
//    [self.iconView setImage:[UIImage imageNamed:@"task_icon"]];
}

+ (CGFloat)heightWithModel:(RouteModel *)model{
    RouteViewCell *cell = [[RouteViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RouteViewCell"];
    cell.model = model;
    [cell layoutIfNeeded];
    
    return CGRectGetMaxY(cell.scanView.frame) +20;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(18);
        make.left.equalTo(self).offset(20);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(0);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self.remarkView.mas_left).offset(-80);
        make.height.equalTo(@50);
    }];
    
    [self.scanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.remarkView.mas_left).offset(-40);
        make.centerY.mas_equalTo(self).offset(0);
//        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
//        make.height.mas_equalTo(@40);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.top.mas_equalTo(self).offset(20);
        make.height.mas_equalTo(XMHeight(74));
        make.width.mas_equalTo(self.iconView.mas_height).multipliedBy(1.0);
    }];
    
    [self.remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self).offset(0);
        make.right.mas_equalTo(self).offset(-20);
        make.width.mas_equalTo(XMWidth(74));
        //        make.right.equalTo(self.iconView.mas_left).offset(-20);
//        make.height.mas_equalTo(XMHeight(74));
    }];
    
    [self.remarkLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-30);
        make.width.equalTo(@85);
        if (IS_IPHONE_5) {
            make.width.equalTo(@55);

        }
        //        make.height.equalTo(@30);
        
    }];
}
@end
